#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <vector>

class ProcessKiller {
private:
    std::vector<pid_t> find_processes_by_name(const std::string& process_name) {
        std::vector<pid_t> pids;
        DIR* proc_dir = opendir("/proc");
        
        if (proc_dir == nullptr) {
            std::cerr << "无法打开 /proc 目录" << std::endl;
            return pids;
        }
        
        struct dirent* entry;
        while ((entry = readdir(proc_dir)) != nullptr) {
            // 检查是否为数字目录（进程ID）
            if (entry->d_type == DT_DIR) {
                std::string dirname = entry->d_name;
                bool is_number = true;
                
                for (char c : dirname) {
                    if (!isdigit(c)) {
                        is_number = false;
                        break;
                    }
                }
                
                if (is_number && !dirname.empty()) {
                    pid_t pid = std::stoi(dirname);
                    
                    // 读取进程的命令行
                    std::string cmdline_path = "/proc/" + dirname + "/cmdline";
                    std::ifstream cmdline_file(cmdline_path);
                    
                    if (cmdline_file.is_open()) {
                        std::string cmdline;
                        std::getline(cmdline_file, cmdline);
                        cmdline_file.close();
                        
                        // 检查命令行是否包含目标进程名
                        if (cmdline.find(process_name) != std::string::npos) {
                            pids.push_back(pid);
                        }
                    }
                }
            }
        }
        
        closedir(proc_dir);
        return pids;
    }
    
    bool kill_process(pid_t pid) {
        std::cout << "正在终止进程 PID: " << pid << std::endl;
        
        // 首先尝试发送 SIGTERM 信号
        if (kill(pid, SIGTERM) == 0) {
            std::cout << "已发送 SIGTERM 信号到进程 " << pid << std::endl;
            
            // 等待一段时间让进程正常退出
            sleep(2);
            
            // 检查进程是否还存在
            if (kill(pid, 0) == 0) {
                std::cout << "进程仍在运行，发送 SIGKILL 信号..." << std::endl;
                if (kill(pid, SIGKILL) == 0) {
                    std::cout << "已发送 SIGKILL 信号到进程 " << pid << std::endl;
                    return true;
                } else {
                    std::cerr << "无法发送 SIGKILL 信号到进程 " << pid << std::endl;
                    return false;
                }
            } else {
                std::cout << "进程 " << pid << " 已正常退出" << std::endl;
                return true;
            }
        } else {
            std::cerr << "无法发送 SIGTERM 信号到进程 " << pid << std::endl;
            return false;
        }
    }
    
public:
    void kill_auto_git_push() {
        std::cout << "=== 自动Git推送进程终止工具 ===" << std::endl;
        
        std::vector<pid_t> pids = find_processes_by_name("auto_git_push");
        
        if (pids.empty()) {
            std::cout << "未找到运行中的 auto_git_push 进程" << std::endl;
            return;
        }
        
        std::cout << "找到 " << pids.size() << " 个 auto_git_push 进程:" << std::endl;
        
        for (pid_t pid : pids) {
            std::cout << "  PID: " << pid << std::endl;
        }
        
        char confirm;
        std::cout << "\n是否确认终止这些进程? (y/n): ";
        std::cin >> confirm;
        
        if (confirm == 'y' || confirm == 'Y') {
            int success_count = 0;
            for (pid_t pid : pids) {
                if (kill_process(pid)) {
                    success_count++;
                }
            }
            
            std::cout << "\n成功终止 " << success_count << "/" << pids.size() << " 个进程" << std::endl;
            
            if (success_count > 0) {
                std::cout << "auto_git_push 后台服务已停止" << std::endl;
            }
        } else {
            std::cout << "操作已取消" << std::endl;
        }
    }
    
    void show_running_processes() {
        std::cout << "=== 查看运行中的 auto_git_push 进程 ===" << std::endl;
        
        std::vector<pid_t> pids = find_processes_by_name("auto_git_push");
        
        if (pids.empty()) {
            std::cout << "未找到运行中的 auto_git_push 进程" << std::endl;
        } else {
            std::cout << "找到 " << pids.size() << " 个运行中的 auto_git_push 进程:" << std::endl;
            for (pid_t pid : pids) {
                std::cout << "  PID: " << pid << std::endl;
                
                // 显示进程启动时间
                std::string stat_path = "/proc/" + std::to_string(pid) + "/stat";
                std::ifstream stat_file(stat_path);
                if (stat_file.is_open()) {
                    std::string line;
                    std::getline(stat_file, line);
                    std::cout << "    状态: 运行中" << std::endl;
                    stat_file.close();
                }
            }
        }
    }
    
    void show_menu() {
        int choice;
        
        while (true) {
            std::cout << "\n=== auto_git_push 进程管理工具 ===" << std::endl;
            std::cout << "1. 查看运行中的进程" << std::endl;
            std::cout << "2. 终止 auto_git_push 进程" << std::endl;
            std::cout << "3. 退出" << std::endl;
            std::cout << "请选择选项 (1-3): ";
            
            std::cin >> choice;
            
            switch (choice) {
                case 1:
                    show_running_processes();
                    break;
                case 2:
                    kill_auto_git_push();
                    break;
                case 3:
                    std::cout << "再见！" << std::endl;
                    return;
                default:
                    std::cout << "无效选择，请重试。" << std::endl;
                    break;
            }
        }
    }
};

int main() {
    ProcessKiller killer;
    killer.show_menu();
    return 0;
}
